--
-- Author: shunguo.chen
-- Date: 2017-11-02 16:06:25
--
-- PlayerLvUpView  角色升级时弹出的奖励展示界面

local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PlayerLvUpScene

local TITLE_STR = L("LevelUpto")

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			self:closeView()
		end)

	self._renders = {0,0,0}
	for i=1,3 do
		local render = IconRender.create(self._ui["Panel/icon_" .. i])
		render:setTipEnable(true)
		render:setTipUnShowOrigin(true)
		self._renders[i] = render
	end
end

function ClassRef:show()
	self.super.show(self)

	self._timeline:setLastFrameCallFunc(function()
			-- 动画结束之后检查引导
			self:checkGuide()
		end)
end

-- 检查是否触发了引导
function ClassRef:checkGuide()
	local params = {
		scene = Constant.FUNC_TYPE_PLAYER_UP,
		sceneSelf = self,
		getFunc = self.getGuideNodeFunc,
		nextFunc = self.checkGuide
	}
	EM:notify("checkGuide", params)
end

-- 获取引导node
function ClassRef:getGuideNodeFunc(id)

end

function ClassRef:refresh(level, attrs, items)
	self._ui.tlt:setString(string.format(TITLE_STR, level))

	for i,render in ipairs(self._renders) do
		local data = items[i]
		render:setVisible(data ~= nil)
		if data then
			render:setData(data)
			local charNum = app:isLanguageCN() and 5 or 12
			render:getChildByName("name"):setString(data:getName(false, charNum))
		end
	end

	for i,attr in ipairs(attrs) do
		self._ui.ListView:pushBackCustomItem(self:_createRender(attr))
	end

end

function ClassRef:_createRender(attr)
	local render = cc.CSLoader:createNode(Res.PlayerLvUpNode)
	local panel = render:getChildByName("Panel")
	render:setContentSize(panel:getContentSize())

	panel:getChildByName("tlt"):setString(attr.name)
	panel:getChildByName("num_1"):setString(attr.old)

	local num_2 = panel:getChildByName("num_2")
	num_2:setString(attr.new)
	if attr.new > attr.old then
		num_2:setColor(cc.c3b(192, 255, 0))
	end
	return render
end

return ClassRef